package com.freework.sys.permissions.system.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.freework.sys.permissions.external.exception.AppException;
import com.freework.sys.permissions.external.service.SessionInfo;
import com.freework.sys.permissions.external.service.UserIdentityServiceI;
import com.freework.sys.permissions.system.vo.SysFuncVO;
import com.freework.sys.permissions.system.vo.UserVO;
import com.freework.tree.TreeDatas;
import com.freework.tree.TreeReadDataService;
import com.freework.tree.TreeReadDataServiceI;
import com.freework.tree.TreeStructureService;
import com.freework.tree.TreeStructureServiceI;
import com.freework.tree.TreeURL;

import constant.SessionName;

@Controller
public class LoginController {

	@Resource(name="freework.sys.permissions.UserIdentityService")
	UserIdentityServiceI userService=null;
	TreeReadDataServiceI treeReadData=new TreeReadDataService();
	TreeStructureServiceI treeStructure=new TreeStructureService();
	public void setUserService(UserIdentityServiceI userService) {
		this.userService = userService;
	}

	@RequestMapping("/permission/LoginController/login.do")
	public String login(String usercode,String password,HttpServletRequest request){
		try {
		UserVO user=userService.login(usercode, password);
		request.getSession().setAttribute(SessionName.USER_INFO, user);
		return "sys/index";
		} catch (AppException e) {
			//e.printStackTrace();
			return null;
		}
		
	}
	@RequestMapping("/permission/LoginController/menu.do")
	@ResponseBody()
	public List<TreeDatas> menu(){
		Integer userid=SessionInfo.getUser().getUserid();
		List<SysFuncVO> list=userService.findUserMenu(userid, 0);
		List<TreeDatas> treelist=treeReadData.getBornTree(list, "funcid", "pfuncid", "funcname", new TreeURL(){
			public String getURL(Object data) {
				SysFuncVO vo=(SysFuncVO) data;
				if(vo.getModuleurl()==null||"".equals(vo.getModuleurl()))
					return "";
				return vo.getModuleurl()+"?menuid="+vo.getFuncid();
			}
			
		}, "funccode");
		treelist=treeStructure.toBornTree(treelist, 0);
		
		return treelist;
		
	} 
	
	
	
}
